package route

import (
	"custom-oracledb-exporter/config"
	"custom-oracledb-exporter/pkg"
	"log"
	"net/http"

	"github.com/prometheus/client_golang/prometheus/promhttp"
)

// HomeHandler 返回首页页面
func homeHandler(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "static/index.html")
}

func InitRoute() {
	http.HandleFunc("/", homeHandler)
	// MetricsHandler 返回 Prometheus 采集数据
	metricsHandle := promhttp.Handler()
	authMetricsHandle := pkg.BasicAuthMiddleware(metricsHandle)

	if config.C.Auth.Enable {
		log.Println("Basic Auth enabled")
		http.Handle("/metrics", authMetricsHandle)
	} else {
		log.Println("Basic Auth Disabled")
		http.Handle("/metrics", metricsHandle)
	}
	log.Printf("Listening and serving HTTP on %s", config.C.Server.GetPort())
	http.ListenAndServe(config.C.Server.GetPort(), nil)

}
